﻿using System;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCM.UI.ChildWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;

namespace Baosight.ZNH.ZMCM.UI
{
    public partial class ZMCM12 : EFViewCmes
    {

        public ZMCM12()
        {
            InitializeComponent();
            this.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 初始化页面构造
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
        }

        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            _view.Args.Add("fdDepotId", this.fdDepotId.Value.ToString());//外键赋值
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 新增操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            string status = this.fdDepotStatus.Value == null ? "" : this.fdDepotStatus.Value.ToString();
            if ("停用".Equals(status))
            {
                //按钮失效
                Button _button = sender as Button;
                _button.IsEnabled = false;
                base.TempButton = _button;
                base.OpenEFWindowAlert("库房停用状态下不能添加新成员权限！", true);
                return;
            }
            else
            {
                if (this.fdDepotId.Value != null)
                {
                    EIInfo eiInfo = new EIInfo();
                    EIBlock block = eiInfo.AddBlock("peopleList");
                    block.SetColumns(this.grid.EIBlock.GetColumns());
                    block.AddRows(this.grid.EIBlock.GetRows());
                    base.OnInsertButtonClickPopup(eiInfo, "人员权限-新增", "insert", null, sender, e);
                }
                else
                {
                    Button _button = sender as Button;
                    _button.IsEnabled = false;
                    base.TempButton = _button;
                    base.OpenEFWindowAlert("请先选择库房添加人员！", true);
                    return;
                }
            } 
        }

        /// <summary>
        /// 从表删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnDeleteButtonClick("ZMCM12", "delete", sender, e);
        }

        /// <summary>
        /// 从表新增或修改之前的数据传输
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        /// <param name="_args"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            LimitsForPeople _peoplt = new LimitsForPeople(_dialogType);//构造一个新的修改页面
            
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.empName"));

            _peoplt.EIDataContext = new Platform.Core.UI.EIDataContext();
            _peoplt.EIDataContext.Update(_eIInfo);

            if (_dialogType == "insert")
            {
                _peoplt.fdDepotId.Value = this.fdDepotId.Value;  
            }

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = _peoplt;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(PopupWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 从表弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PopupWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新从表Grid区域
                this.grid.Refresh();
            }
            //按钮生效
            base.TempButtonOpen(true);
        }
    }
}
